﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.IO;

namespace Commander.program
{
    /// <summary>
    /// Простейший текстовий редактор
    /// </summary>
    public partial class Notepad : Window
    {
        private string _NameFile;
        private bool _IsReadOnly;

        public Notepad()
        {
            InitializeComponent();
            _NameFile = null;
            _IsReadOnly = false;
        }

        /// <summary>
        /// Полний путь к файлу которий надо просмотреть
        /// </summary>
        public string NameFile
        {
            set
            {
                _NameFile = value;
            }
        }

        public bool IsReadOnly
        {
            set
            {
                _IsReadOnly = value;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (_NameFile != null)
            {
                if (File.Exists(_NameFile))
                {
                    try
                    {
                        StreamReader read = new StreamReader(_NameFile);
                        textBox1.Text = read.ReadToEnd();
                        read.Close();
                        statusText.Content = _NameFile;
                        if (_IsReadOnly)
                        {
                            textBox1.IsReadOnly = true;
                            menu1.Visibility = Visibility.Hidden;
                        }
                    }
                    catch (Exception a)
                    {
                        MessageBox.Show(a.ToString());
                    }
                }
            }
            Keyboard.Focus(textBox1);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                StreamWriter write = new StreamWriter(_NameFile);
                write.Write(textBox1.Text);
                write.Close();
            }
            catch (Exception a)
            {
                MessageBox.Show(a.ToString());
            }
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string namefile = dlg.FileName;
                try
                {
                    StreamReader read = new StreamReader(namefile);
                    textBox1.Text = read.ReadToEnd();
                    read.Close();
                    statusText.Content = namefile;
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.ToString());
                }
            }
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                try
                {
                    FileStream fs = new FileStream(filename, FileMode.Create);
                    StreamWriter outstr = new StreamWriter(fs);
                    outstr.Write(textBox1.Text);
                    outstr.Close();
                    fs.Close();
                    statusText.Content = _NameFile;
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.ToString());
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }
    }
}
